﻿using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDBSample.Common.Utils;
using MongoDBSample.Models.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MongoDBSample.DataAccess
{
    public class DataManager
    {
        /// <summary>
        /// Query Mongo DB and get all attachments
        /// </summary>
        public IList<Attachment> GetAllttachments()
        {
            MongoHelper<Attachment> attachments = new MongoHelper<Attachment>();
            return attachments.Collection.FindAll().ToList();
        }

        public void AddAttachment(Attachment attachment)
        {
            MongoHelper<Attachment> attachments = new MongoHelper<Attachment>();
            attachments.Collection.Save(attachment);
        }

        public void DeleteAttachment(ObjectId attachmentId)
        {
            MongoHelper<Attachment> attachments = new MongoHelper<Attachment>();
            attachments.Collection.Remove(Query.EQ("_id", attachmentId));
        }


        public Attachment GetAttachment(ObjectId id)
        {
            MongoHelper<Attachment> attachments = new MongoHelper<Attachment>();
            var attachment = attachments.Collection.Find(Query.EQ("_id", id)).Single();
            return attachment;
        }
    }
}